﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using System.Data;
using App.Data;

public partial class UserControls_SpecialOfferList : BaseUserControl
{
    private const String CUSTOMER_ID = "CustomerID";
    private const String PRIVATE_LIST = "PrivateList";
    /// <summary>
    /// Determines for which Customer the list should be populated. Provide 0 to List all active Special Offers from Different Customers.
    /// </summary>
    public int CustomerID { get; set; }

    public bool IsPrivateList { get; set; }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState[CUSTOMER_ID] = this.CustomerID;
            ViewState[PRIVATE_LIST] = this.IsPrivateList.ToString();
            BindSpecialOfferList(1);            
        }
    }
    protected String GetCompanyLinkOrName(String customerName, int customerID, bool isIndividual)
    {
        //<a href="/Pages/Public/CompanyProfile.aspx?<%# AppConstants.QueryString.ID %>=<%# DataBinder.Eval(Container.DataItem, "CustomerID")%>" target="_blank"><%# WebUtil.FormatData(DataBinder.Eval(Container.DataItem, "Name"))%></a>
        if (isIndividual)
            return customerName.HtmlEncode();
        else
            return String.Format("<a href='/Pages/Public/CompanyProfile.aspx?{0}={1}' target='_blank'>{2}</a>"
                , AppConstants.QueryString.ID
                , customerID
                , customerName);
    }
    protected void BindSpecialOfferList(int pageNumber)
    {
        ///Patch for Postback handling
        this.CustomerID = ViewState[CUSTOMER_ID].ToString().ToInt();
        this.IsPrivateList = String.Compare(ViewState[PRIVATE_LIST].ToString(), "true", true) == 0 ? true : false;

        UtilityDAO dao = new UtilityDAO();
        int totalRecord = 0;
        DataSet ds = null;        
        if(this.CustomerID == 0 && WebUtil.IsAdministrator()) /// Full List for Administrator
            ds = dao.GetPagedData(AppSQL.GET_PAGED_SPECIAL_OFFERS_LIST, null, pageNumber, ConfigReader.PublicSpecialOfferListSize, out totalRecord);
        else if(this.CustomerID == 0) ///Listing for Public Page
            ds = dao.GetPagedData(AppSQL.GET_PAGED_ACTIVE_SPECIAL_OFFERS_LIST, null, pageNumber, ConfigReader.PublicSpecialOfferListSize, out totalRecord);
        else /// Listing for Individual or Specific Company
            ds = dao.GetPagedData(AppSQL.GET_PAGED_SPECIAL_OFFERS_BY_CUSTOMER, new [] {new App.Data.DbParameter("@CustomerID", this.CustomerID)}, pageNumber, ConfigReader.PrivateSpecialOfferListSize, out totalRecord);
        
        rptSpecialOfferList.DataSource = ds.Tables[0];
        rptSpecialOfferList.DataBind();

        ///Bind the Pager Control
        ucSpecialOfferListPager.TotalRecord = totalRecord;
        ucSpecialOfferListPager.PageNo = pageNumber;
        ucSpecialOfferListPager.PageSize = this.CustomerID == 0 ? ConfigReader.PublicSpecialOfferListSize : ConfigReader.PrivateSpecialOfferListSize;
        ucSpecialOfferListPager.TotalRecordMessage = "Total {0} Special Offer(s) Found.";
        ucSpecialOfferListPager.DataBind();
        //DataBind();
    }
    //public override void DataBind()
    //{
    //    base.DataBind();
    //}

    protected void ucSpecialOfferListPager_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindSpecialOfferList(e.PageIndex);
    }
}